package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.make.GetMakeMethodBo;
import com.lframework.xingyun.basedata.bo.make.ProductMakeBo;
import com.lframework.xingyun.basedata.bo.make.QueryMakeMethodBo;
import com.lframework.xingyun.basedata.entity.MakeMethod;
import com.lframework.xingyun.basedata.service.make.MakeMethodService;
import com.lframework.xingyun.basedata.service.make.ProductMakeService;
import com.lframework.xingyun.basedata.vo.make.CreateMakeMethodVo;
import com.lframework.xingyun.basedata.vo.make.CreateProductMakeVo;
import com.lframework.xingyun.basedata.vo.make.QueryMakeMethodVo;
import com.lframework.xingyun.basedata.vo.make.UpdateMakeMethodVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 制作方法表 Controller
 *
 */
@Api(tags = "制作方法表")
@Validated
@RestController
@RequestMapping("/basedata/make")
public class MakeMethodController extends DefaultBaseController {

    @Autowired
    private MakeMethodService makeMethodService;

    @Autowired
    private ProductMakeService productMakeService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:make:query"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryMakeMethodBo>> query(@Valid QueryMakeMethodVo vo) {

        PageResult<MakeMethod> pageResult = makeMethodService.query(getPageIndex(vo), getPageSize(vo), vo);

        List<MakeMethod> datas = pageResult.getDatas();
        List<QueryMakeMethodBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryMakeMethodBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:make:query"})
    @GetMapping
    public InvokeResult<GetMakeMethodBo> get(@NotBlank(message = "id不能为空！") String id) {

        MakeMethod data = makeMethodService.findById(id);
        if (data == null) {
            throw new DefaultClientException("制作方法表不存在！");
        }

        GetMakeMethodBo result = new GetMakeMethodBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:make:add"})
    @PostMapping
    public InvokeResult<Void> create(@Valid @RequestBody CreateMakeMethodVo vo) {

        makeMethodService.create(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @HasPermission({"base-data:make:modify"})
    @PutMapping
    public InvokeResult<Void> update(@Valid @RequestBody UpdateMakeMethodVo vo) {

        makeMethodService.update(vo);

        makeMethodService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }

    /**
     * 保存商品与制作方法关系
     * @param makeId
     * @param vos
     * @return
     */
    @ApiOperation("保存商品与制作方法关系")
    @HasPermission({"base-data:make:product:add"})
    @PostMapping("product/{makeId}")
    public InvokeResult<Void> createProductMake(@PathVariable String makeId, @Valid @RequestBody List<CreateProductMakeVo> vos) {

        productMakeService.create(makeId, vos);

        return InvokeResultBuilder.success();
    }

    /**
     * 根据制作方法ID获取关联商品
     * @param makeId
     * @return
     */
    @ApiOperation("根据制作方法ID获取关联商品")
    @HasPermission({"base-data:make:query"})
    @GetMapping("product/{makeId}")
    public InvokeResult<List<ProductMakeBo>> getProductMake(@PathVariable String makeId) {

        List<ProductMakeBo> results = productMakeService.selectByMakeId(makeId).stream()
                .map(ProductMakeBo::new).collect(Collectors.toList());

        return InvokeResultBuilder.success(results);
    }
}

